{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Episode 06: Computing in the Cloud.\n",
    "\n",
    "### In Episode 06 we re-ran MovieStatsFlow on AWS using using remote storage, metadata, and compute. This notebook shows how you can access your artifacts from anywhere. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Import the metaflow client"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from metaflow import Flow, get_metadata\n",
    "import matplotlib.pyplot as plt\n",
    "print(\"Current metadata provider: %s\" % get_metadata())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Get the latest successful run of MovieStatsFlow"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "run = Flow('MovieStatsFlow').latest_successful_run\n",
    "print(\"Using run: %s\" % str(run))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## You can get all of your data artifacts from the remote datastore, even the 'movies.csv' input file. Lets print the last line of the file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "movies_csv = run.data.movie_data\n",
    "lines = [line for line in movies_csv.split('\\n') if line]\n",
    "print(\"The best movie ever made:\")\n",
    "print(lines[-1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Get the genre specific movie statistics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "genre_stats = run.data.genre_stats"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create a bar plot of the median gross box office for the top-5 grossing genres"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get median for each genre\n",
    "data = [(genre, data['quartiles'][1]) \\\n",
    "        for genre, data \\\n",
    "        in genre_stats.items()]\n",
    "\n",
    "# Sort and unpack into a list of labels, and medians\n",
    "genre, median = zip(*[(genre, median)\\\n",
    "                      for genre, median\\\n",
    "                      in sorted(data, key=lambda pair: pair[1])])\n",
    "\n",
    "# Create the bar plot\n",
    "plt.bar(genre[-5:], median[-5:], align='center', alpha=0.5)\n",
    "plt.ylabel(\"Gross Box office (US Dollars)\")\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
